{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/usr/local/lib/python3.5/dist-packages/sklearn/cross_validation.py:41: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20.\n",
      "  \"This module will be removed in 0.20.\", DeprecationWarning)\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "import time\n",
    "import os\n",
    "import re\n",
    "import collections\n",
    "import random\n",
    "import pickle\n",
    "from sklearn.cross_validation import train_test_split"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "maxlen = 20\n",
    "location = os.getcwd()\n",
    "learning_rate = 1e-8\n",
    "batch = 100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('dataset-emotion.p', 'rb') as fopen:\n",
    "    df = pickle.load(fopen)\n",
    "with open('vector-emotion.p', 'rb') as fopen:\n",
    "    vectors = pickle.load(fopen)\n",
    "with open('dataset-dictionary.p', 'rb') as fopen:\n",
    "    dictionary = pickle.load(fopen)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "label = np.unique(df[:,1])\n",
    "train_X, test_X, train_Y, test_Y = train_test_split(df[:,0], df[:, 1].astype('int'), test_size = 0.2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def sinusoidal_positional_encoding(inputs, num_units, zero_pad=False, scale=False):\n",
    "    T = inputs.get_shape().as_list()[1]\n",
    "    position_idx = tf.tile(tf.expand_dims(tf.range(T), 0), [tf.shape(inputs)[0], 1])\n",
    "    position_enc = np.array([[pos / np.power(10000, 2.*i/num_units) for i in range(num_units)] for pos in range(T)])\n",
    "    position_enc[:, 0::2] = np.sin(position_enc[:, 0::2])\n",
    "    position_enc[:, 1::2] = np.cos(position_enc[:, 1::2])\n",
    "    lookup_table = tf.convert_to_tensor(position_enc, tf.float32)\n",
    "    if zero_pad:\n",
    "        lookup_table = tf.concat([tf.zeros([1, num_units]), lookup_table[1:, :]], axis=0)\n",
    "    outputs = tf.nn.embedding_lookup(lookup_table, position_idx)\n",
    "    if scale:\n",
    "        outputs = outputs * num_units ** 0.5\n",
    "    return outputs\n",
    "\n",
    "class Model:\n",
    "    def __init__(self, seq_len, dimension_input, dimension_output, learning_rate):\n",
    "        self.X = tf.placeholder(tf.float32, [None, seq_len, dimension_input])\n",
    "        self.Y = tf.placeholder(tf.float32, [None, dimension_output])\n",
    "        x = self.X\n",
    "        x += sinusoidal_positional_encoding(x, dimension_input)\n",
    "        masks = tf.sign(self.X[:,:,0])\n",
    "        align = tf.squeeze(tf.layers.dense(x, 1, tf.tanh), -1)\n",
    "        paddings = tf.fill(tf.shape(align), float('-inf'))\n",
    "        align = tf.where(tf.equal(masks, 0), paddings, align)\n",
    "        align = tf.expand_dims(tf.nn.softmax(align), -1)\n",
    "        x = tf.squeeze(tf.matmul(tf.transpose(x, [0,2,1]), align), -1)\n",
    "        self.logits = tf.layers.dense(x, dimension_output)\n",
    "        self.cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = self.logits, labels = self.Y))\n",
    "        self.optimizer = tf.train.AdamOptimizer(learning_rate = learning_rate).minimize(self.cost)\n",
    "        self.correct_pred = tf.equal(tf.argmax(self.logits, 1), tf.argmax(self.Y, 1))\n",
    "        self.accuracy = tf.reduce_mean(tf.cast(self.correct_pred, tf.float32))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "'unwarrentedly'\n",
      "epoch: 0 , pass acc: 0 , current acc: 0.1381512606049142\n",
      "time taken: 35.512893199920654\n",
      "epoch: 1 , training loss: nan , training acc: 0.13736652690039006 , valid loss: nan , valid acc: 0.1381512606049142\n",
      "'unwarrentedly'\n",
      "time taken: 34.82982063293457\n",
      "epoch: 2 , training loss: nan , training acc: 0.13736652690039006 , valid loss: nan , valid acc: 0.1381512606049142\n",
      "'unwarrentedly'\n",
      "time taken: 34.818602323532104\n",
      "epoch: 3 , training loss: nan , training acc: 0.13736652690039006 , valid loss: nan , valid acc: 0.1381512606049142\n",
      "'unwarrentedly'\n",
      "time taken: 34.782310247421265\n",
      "epoch: 4 , training loss: nan , training acc: 0.13736652690039006 , valid loss: nan , valid acc: 0.1381512606049142\n",
      "'unwarrentedly'\n",
      "time taken: 34.77112555503845\n",
      "epoch: 5 , training loss: nan , training acc: 0.13736652690039006 , valid loss: nan , valid acc: 0.1381512606049142\n",
      "'unwarrentedly'\n",
      "time taken: 34.748547077178955\n",
      "epoch: 6 , training loss: nan , training acc: 0.13736652690039006 , valid loss: nan , valid acc: 0.1381512606049142\n",
      "'unwarrentedly'\n",
      "time taken: 34.836182594299316\n",
      "epoch: 7 , training loss: nan , training acc: 0.13736652690039006 , valid loss: nan , valid acc: 0.1381512606049142\n",
      "'unwarrentedly'\n",
      "time taken: 34.811713218688965\n",
      "epoch: 8 , training loss: nan , training acc: 0.13736652690039006 , valid loss: nan , valid acc: 0.1381512606049142\n",
      "'unwarrentedly'\n",
      "time taken: 34.80507969856262\n",
      "epoch: 9 , training loss: nan , training acc: 0.13736652690039006 , valid loss: nan , valid acc: 0.1381512606049142\n",
      "'unwarrentedly'\n",
      "time taken: 34.85770010948181\n",
      "epoch: 10 , training loss: nan , training acc: 0.13736652690039006 , valid loss: nan , valid acc: 0.1381512606049142\n",
      "'unwarrentedly'\n",
      "time taken: 34.83849835395813\n",
      "epoch: 11 , training loss: nan , training acc: 0.13736652690039006 , valid loss: nan , valid acc: 0.1381512606049142\n",
      "break epoch: 11\n"
     ]
    }
   ],
   "source": [
    "tf.reset_default_graph()\n",
    "sess = tf.InteractiveSession()\n",
    "model = Model(maxlen, vectors.shape[1], label.shape[0], learning_rate)\n",
    "sess.run(tf.global_variables_initializer())\n",
    "saver = tf.train.Saver(tf.global_variables())\n",
    "dimension = vectors.shape[1]\n",
    "EARLY_STOPPING, CURRENT_CHECKPOINT, CURRENT_ACC, EPOCH = 10, 0, 0, 0\n",
    "while True:\n",
    "    lasttime = time.time()\n",
    "    if CURRENT_CHECKPOINT == EARLY_STOPPING:\n",
    "        print('break epoch:', EPOCH)\n",
    "        break\n",
    "    train_acc, train_loss, test_acc, test_loss = 0, 0, 0, 0\n",
    "    for i in range(0, (train_X.shape[0] // batch) * batch, batch):\n",
    "        batch_x = np.zeros((batch, maxlen, dimension))\n",
    "        batch_y = np.zeros((batch, len(label)))\n",
    "        for k in range(batch):\n",
    "            tokens = train_X[i + k].split()[:maxlen]\n",
    "            emb_data = np.zeros((maxlen, dimension), dtype = np.float32)\n",
    "            for no, text in enumerate(tokens[::-1]):\n",
    "                try:\n",
    "                    emb_data[-1 - no, :] += vectors[dictionary[text], :]\n",
    "                except Exception as e:\n",
    "                    print(e)\n",
    "                    continue\n",
    "            batch_y[k, int(train_Y[i + k])] = 1.0\n",
    "            batch_x[k, :, :] = emb_data[:, :]\n",
    "        loss, _ = sess.run([model.cost, model.optimizer], feed_dict = {model.X : batch_x, model.Y : batch_y})\n",
    "        train_loss += loss\n",
    "        train_acc += sess.run(model.accuracy, feed_dict = {model.X : batch_x, model.Y : batch_y})\n",
    "    \n",
    "    for i in range(0, (test_X.shape[0] // batch) * batch, batch):\n",
    "        batch_x = np.zeros((batch, maxlen, dimension))\n",
    "        batch_y = np.zeros((batch, len(label)))\n",
    "        for k in range(batch):\n",
    "            tokens = test_X[i + k].split()[:maxlen]\n",
    "            emb_data = np.zeros((maxlen, dimension), dtype = np.float32)\n",
    "            for no, text in enumerate(tokens[::-1]):\n",
    "                try:\n",
    "                    emb_data[-1 - no, :] += vectors[dictionary[text], :]\n",
    "                except:\n",
    "                    continue\n",
    "            batch_y[k, int(test_Y[i + k])] = 1.0\n",
    "            batch_x[k, :, :] = emb_data[:, :]\n",
    "        loss, acc = sess.run([model.cost, model.accuracy], feed_dict = {model.X : batch_x, model.Y : batch_y})\n",
    "        test_loss += loss\n",
    "        test_acc += acc\n",
    "        \n",
    "    train_loss /= (train_X.shape[0] // batch)\n",
    "    train_acc /= (train_X.shape[0] // batch)\n",
    "    test_loss /= (test_X.shape[0] // batch)\n",
    "    test_acc /= (test_X.shape[0] // batch)\n",
    "    if test_acc > CURRENT_ACC:\n",
    "        print('epoch:', EPOCH, ', pass acc:', CURRENT_ACC, ', current acc:', test_acc)\n",
    "        CURRENT_ACC = test_acc\n",
    "        CURRENT_CHECKPOINT = 0\n",
    "        saver.save(sess, os.getcwd() + \"/model-rnn-vector.ckpt\")\n",
    "    else:\n",
    "        CURRENT_CHECKPOINT += 1\n",
    "    EPOCH += 1\n",
    "    print('time taken:', time.time()-lasttime)\n",
    "    print('epoch:', EPOCH, ', training loss:', train_loss, ', training acc:', train_acc, ', valid loss:', test_loss, ', valid acc:', test_acc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
